package zhongbo.java.air.dictionary.service.Impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AlertZoneMapper;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.service.AlertZoneServer;
import zhongbo.java.air.common.exception.MyException;

import java.util.Date;

@Service
public class AlertZoneServiceImpl implements AlertZoneServer {

    @Autowired
    private AlertZoneMapper alertZoneMapper;


    @Override
    public AlertZone addAlertZone(AlertZone alertZone) throws MyException {
        Date dNow = new Date();
//        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        alertZone.setCreatTime(dNow);
        alertZone.setUpdateTime(dNow);
        if (!alertZone.getZoneCode().matches("^[A-Z]+_(NOFLY|RESTRICTED|TEMPORARY])_\\d{3}$")) {
            throw new MyException("区域业务编号格式错误");
        }
        AlertZone alertZone1 = alertZoneMapper.selectByzoneCode(alertZone.getZoneCode());
        if (alertZone1 != null) {
            throw new MyException("添加区域已存在");
        }
        if (!alertZone.getZoneType().matches("^(NOFLY|RESTRICTED|TEMPORARY)$")) {
            throw new MyException("区域类型错误");
        }

        int ok = alertZoneMapper.insert(alertZone);
        if (ok <= 0) {
            throw new MyException("警戒线信息添加失败");
        }
        return alertZoneMapper.selectByzoneCode(alertZone.getZoneCode());
    }

    @Override
    public String deleteAlertZone(String id) throws MyException {
        int ok = alertZoneMapper.deleteById(id);
        if (ok <= 0) {
            throw new MyException("警戒线信息删除失败");
        }
        return String.valueOf(ok);
    }

    @Override
    public AlertZone updateAlertZone(AlertZone alertZone) throws MyException {
        if (alertZone.getZoneCode() == null) {
            throw new MyException("警戒线业务编号不能为空");
        }
        if (!alertZone.getZoneCode().matches("^[A-Z]+_(NOFLY|RESTRICTED|TEMPORARY])_\\d{3}$")) {
            throw new MyException("区域业务编号格式错误");
        }
        if (alertZone.getZoneType() != null) {
            if (!alertZone.getZoneType().matches("^(NOFLY|RESTRICTED|TEMPORARY)$")) {
                throw new MyException("区域类型错误");
            }
        }
        alertZone.setUpdateTime(new Date());
        int ok = alertZoneMapper.updateById(alertZone);
        if (ok <= 0) {
            throw new MyException("警戒线信息修改失败");
        }
        return alertZoneMapper.selectByzoneCode(alertZone.getZoneCode());

    }

    @Override
    public AlertZone selectAlertZoneById(String id) {
        return alertZoneMapper.selectById(id);
    }

    @Override
    public PageVo<AlertZone> page(Integer pageNumber, Integer pageSize, boolean order, String searchField, String keyword, String sortField) {
        QueryWrapper<AlertZone> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(sortField)) {
            if (order) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        }

        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(searchField)) {
            String[]fieldsArray = searchField.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }

        IPage<AlertZone> pageParam = new Page<>(pageNumber, pageSize);
        IPage<AlertZone> resultPage = alertZoneMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(resultPage);
    }
}
